package com.anyikang.volunteer.sos.volunteer.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

/**
 * Toast工具类
 *
 * @author xw.gao
 * @version 2017/4/21
 * @git http://60.205.139.138:81/root/GSMServerAPP.git
 */
public class ToastUtils {

    private static Toast toast;

    /**
     * Toast
     *
     * @param context
     * @param msg
     */
    public static void showToast(Context context, String msg) {
        if (toast == null) {
            //如果toast第一次创建 makeText创建toast对象
            toast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
        } else {
            //如果toast存在 只需要修改文字
            toast.setText(msg);
        }
        toast.show();
    }

    /**
     * 在子线程展示Toast
     *
     * @param activity
     * @param msg
     */
    public static void showToastOnUiThread(final Activity activity, final String msg) {
        if (activity == null) {
            return;
        }

        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (toast == null) {
                    //如果toast第一次创建 makeText创建toast对象
                    toast = Toast.makeText(activity, msg, Toast.LENGTH_SHORT);
                } else {
                    //如果toast存在 只需要修改文字
                    toast.setText(msg);
                }
                toast.show();
            }
        });
    }


}
